package com.eric.r2d2.pipline;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

public class MongoPipeline extends FilePersistentBase implements Pipeline   {
	MongoDatabase db;
	MongoCollection<BasicDBObject> coll;
	MongoClient mc;
	
	public MongoPipeline(String host,String db,String coll) throws UnknownHostException {
		try {
			this.mc = new MongoClient(new MongoClientURI(host));
		} catch (MongoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.db = mc.getDatabase(db);
        this.coll = this.db.getCollection(coll,BasicDBObject.class);
    }
    
	public void process(ResultItems resultItems, Task task) {
		// TODO Auto-generated method stub
		BasicDBObject json_results = (BasicDBObject) JSON.parse(resultItems.get("results").toString());
		coll.insertOne(json_results);
	}

}
